
package UI;

import ADO.Logs;
import Entidades.SingletonUsuario;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;


public class VNuevoUsuario extends javax.swing.JDialog {

    /** Creates new form VNuevoUsuario */
    public VNuevoUsuario(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jBSalir = new javax.swing.JButton();
        jBIniciar = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jTxtUss = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jPpass1 = new javax.swing.JPasswordField();
        jPpass2 = new javax.swing.JPasswordField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jBSalir.setText("Salir");
        jBSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBSalirActionPerformed(evt);
            }
        });

        jBIniciar.setText("Registrar");
        jBIniciar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBIniciarActionPerformed(evt);
            }
        });
        jBIniciar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jBIniciarKeyPressed(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Registrar Usuario"));

        jLabel1.setText("Nombre de Usuario");

        jLabel2.setText("Contraseña");

        jTxtUss.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTxtUssActionPerformed(evt);
            }
        });

        jLabel3.setText("Repita Contraseña");

        jPpass1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jPpass1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPpass1, javax.swing.GroupLayout.DEFAULT_SIZE, 122, Short.MAX_VALUE)
                    .addComponent(jTxtUss, javax.swing.GroupLayout.DEFAULT_SIZE, 122, Short.MAX_VALUE)
                    .addComponent(jPpass2, javax.swing.GroupLayout.DEFAULT_SIZE, 122, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTxtUss, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jPpass1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 16, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jPpass2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(133, 133, 133)
                        .addComponent(jBIniciar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jBSalir))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jBIniciar)
                    .addComponent(jBSalir))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jBSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBSalirActionPerformed
    this.dispose();
}//GEN-LAST:event_jBSalirActionPerformed

private void jBIniciarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBIniciarActionPerformed

    Soporte.GestorUsuario gu = new Soporte.GestorUsuario();
    if (this.jTxtUss.getText().equalsIgnoreCase("")) {
        JOptionPane.showMessageDialog(rootPane, "FALTA NOMBRE DE USUARIO", "ERROR DE LOG IN", JOptionPane.ERROR_MESSAGE);
    } else if (this.jPpass1.getText().equalsIgnoreCase("")) {
        JOptionPane.showMessageDialog(rootPane, "FALTA CONTRASEÑA DE USUARIO", "ERROR DE LOG IN", JOptionPane.ERROR_MESSAGE);
    } else if (!gu.existeUsuario(this.jTxtUss.getText().trim())) {
        JOptionPane.showMessageDialog(rootPane, "EL USUARIO YA EXISTE", "ERROR DE LOG IN", JOptionPane.ERROR_MESSAGE);
       // existe=true;
    } else if (!this.jPpass1.getText().equalsIgnoreCase(this.jPpass2.getText().trim())) {
        JOptionPane.showMessageDialog(rootPane, "LA CONTRASEÑA ES INCORRECTA", "ERROR DE LOG IN", JOptionPane.ERROR_MESSAGE);
    } else {
        boolean b=true;
        try {                
        gu.crearUsuario(this.jTxtUss.getText(), Entidades.Encriptacion.getMD5(this.jPpass1.getText()));
        } catch (Exception ex) {
            Logger.getLogger(VNuevoUsuario.class.getName()).log(Level.SEVERE, null, ex);
            JOptionPane.showMessageDialog(rootPane, "LA CONTRASEÑA ES INCORRECTA", "ERROR DE LOG IN", JOptionPane.ERROR_MESSAGE);
            b=false;
        }
        if(b){
        JOptionPane.showMessageDialog(rootPane, "EXITOSO", "ERROR DE LOG IN", JOptionPane.ERROR_MESSAGE);
        Logs l= new Logs();
        l.entrada(Logs.opciones.RegistrarUsuario,1);
        }        
    }
    this.dispose();
    
}//GEN-LAST:event_jBIniciarActionPerformed

private void jBIniciarKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jBIniciarKeyPressed
 
}//GEN-LAST:event_jBIniciarKeyPressed

private void jTxtUssActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTxtUssActionPerformed
}//GEN-LAST:event_jTxtUssActionPerformed

private void jPpass1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jPpass1ActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_jPpass1ActionPerformed

  
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBIniciar;
    private javax.swing.JButton jBSalir;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPasswordField jPpass1;
    private javax.swing.JPasswordField jPpass2;
    private javax.swing.JTextField jTxtUss;
    // End of variables declaration//GEN-END:variables
}
